package com.hunan.api.catv.runner;

import com.hunan.api.catv.action.BaseAction;
import com.hunan.api.catv.utils.sensitivewords.SensitivewordFilterUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;


@Component
public class SensitiveWordRunner implements CommandLineRunner {

    public static Logger log = LoggerFactory.getLogger(BaseAction.class);

    @Override
    public void run(String... args) throws Exception {
        log.info("开始加载敏感词库");
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("sensitveword/s/w/sensiwords");
        InputStreamReader read = new InputStreamReader(in, "UTF-8");
        Set<String> set = new HashSet<>();
        try {
            BufferedReader bufferedReader = new BufferedReader(read);
            String txt;
            while ((txt = bufferedReader.readLine()) != null) {
                set.add(txt);
            }
        } catch (Exception e) {
            log.error("加载词库失败!:{}", e);
        } finally {
            read.close();
        }
        log.info("完成加载敏感词库");
        SensitivewordFilterUtil.init(set);
    }
}
